<?php
namespace  App\Http\Controllers\Radius;
use  App\Http\Controllers\Controller;

use App\Order;

use App\Teamqq;
use Illuminate\Http\Request;

use App\Common\ErrorDefine;

use App\Radacct;
use App\Radcheck;
use App\Radgroupcheck;
use App\Radreply;
use App\Radusergroup;
use App\Rad_manage;
use App\Radgroupreply;
use App\Rad_users;
use App\Rad_order;
use DB;

use App\Line;
use App\Agent;


class  AdminController extends Controller
{

    /**
     * @param Request $request
     * 管理员列表
     */

    public function getAdminList(Request $request)
    {
        //  判断数据是否为空
        if(empty($request->all()))
        {
            self::ShowError(ErrorDefine::EMPTY_POST_DATA);
            return;
        }


        //   开户人的 token
        if(!$request->has('token') && !empty($request->input('token')))
        {
            self::ShowError(ErrorDefine::TOKEN_NOT_EXIST);
            return;
        }
        $token = $request->input('token');


        //   get manage userId
        $user = self::getTokenById($token);
        if(empty($user))
        {
            return self::ShowError(ErrorDefine::TOKEN_IS_EXPIRERD);
        }

        //  管理理员权限
        if($user[0]['level'] != 2)
        {
            return self::ShowError(ErrorDefine::PERMISSION_DENIED);
        }

        $admin = Rad_manage::orderBy('created_at','desc')->get();

        foreach($admin as &$value)
        {
            switch($value->level)
            {
                case 1:$value->designation = '普通管理员';break;
                case 2:$value->designation = '超级管理员';break;
                case 3:$value->designation = '客服';break;
            }

        }

        return self::ShowMessage($admin);

    }


    /**
     * @param Request $request
     *管理员信息修改
     */
    public function doAdminUpdate(Request $request)
    {
        //  判断数据是否为空
        if(empty($request->all()))
        {
            self::ShowError(ErrorDefine::EMPTY_POST_DATA);
            return;
        }


        $where = [];

        $where = $request->all();

        $params = [];

        $params['manage_id'] = $where['manage_id'];

        unset($where['manage_id']);


        $ret = Rad_manage::where($params)->update($where);


        if($ret)
        {
            return self::ShowMessage();
        }else{
            return self::ShowError(ErrorDefine::DATABASE_ERROR);
        }

    }


    /**
     *管理员充值
     */
    public function doAdminRecharge(Request $request)
    {
//          判断数据是否为空
        if(empty($request->all()))
        {
            self::ShowError(ErrorDefine::EMPTY_POST_DATA);
            return;
        }

        $manage_id = $request->input('manage_id');
        $money = $request->input('money');

        $manage = Rad_manage::find($manage_id);


        if($money < 0 || !is_numeric($money))
        {
            return self::ShowError(ErrorDefine::INVALID_MONEY);
        }

        $manage->balance = $manage->balance + $money;

        $manage->save();

        return self::ShowMessage();


    }


    //  管理员删除

    public function doAdminDelete(Request $request)
    {
//        判断数据是否为空
        if(empty($request->all()))
        {
            self::ShowError(ErrorDefine::EMPTY_POST_DATA);
            return;
        }

        if(!$request->has('manage_id') && empty($request->input('manage_id')))
        {
            self::ShowError(ErrorDefine::ACCOUNT_ID_NOT_EXIST);
            return;
        }

        $id = $request->input('manage_id');

        Rad_manage::where('manage_id',$id)->delete();

        Rad_users::where('manage_id',$id)->update(['manage_id'=>'0']);

        $qq = Teamqq::where('manage_id',$id)->get();

        if(!empty($qq))
        {
            Teamqq::where('manage_id',$id)->delete();
        }

        return self::ShowMessage();
    }

    /**
     *
     * 管理员添加
     */
    public function doAdminAdd(Request $request)
    {

        $params = [];
        $qq = '';
        $qq_name = '';


//        判断数据是否为空
        if(empty($request->all()))
        {
            self::ShowError(ErrorDefine::EMPTY_POST_DATA);
            return;
        }


        if(!$request->has('username') || empty($request->input('username')))
        {
            self::ShowError(ErrorDefine::INVALID_ACCOUNT);
            return;
        }
        $params['username'] = $request->input('username');



        $users = Rad_manage::where('username',$params['username'])->get()->toArray();

        if(!empty($users))
        {
            self::ShowError(ErrorDefine::ACCOUNT_IS_EXIST);
            return;
        }

        if(!$request->has('password') || empty($request->input('password')))
        {
            self::ShowError(ErrorDefine::INVALID_POSSWORD);
            return;
        }

        $params['password'] = $request->input('password');


        if(!$request->has('primary-day') || empty($request->input('primary-day')))
        {
            self::ShowError(ErrorDefine::INVALID_PRIMARY);
            return;
        }
        $params['primary-day'] = $request->input('primary-day');



        if(!$request->has('primary-week') || empty($request->input('primary-week')))
        {
            self::ShowError(ErrorDefine::INVALID_PRIMARY);
            return;
        }
        $params['primary-week'] = $request->input('primary-week');


        if(!$request->has('primary-month') || empty($request->input('primary-month')))
        {
            self::ShowError(ErrorDefine::INVALID_PRIMARY);
            return;
        }
        $params['primary-month'] = $request->input('primary-month');




        if(!$request->has('middle-day') || empty($request->input('middle-day')))
        {
            self::ShowError(ErrorDefine::INVALID_MIDDLE);
            return;
        }
        $params['middle-day'] = $request->input('middle-day');

        if(!$request->has('middle-week') || empty($request->input('middle-week')))
        {
            self::ShowError(ErrorDefine::INVALID_MIDDLE);
            return;
        }
        $params['middle-week'] = $request->input('middle-week');



        if(!$request->has('middle-month') || empty($request->input('middle-month')))
        {
            self::ShowError(ErrorDefine::INVALID_MIDDLE);
            return;
        }
        $params['middle-month'] = $request->input('middle-month');






        if(!$request->has('height-day') || empty($request->input('height-day')))
        {
            self::ShowError(ErrorDefine::INVALID_HEIGHT);
            return;
        }
        $params['height-day'] = $request->input('height-day');


        if(!$request->has('height-week') || empty($request->input('height-week')))
        {
            self::ShowError(ErrorDefine::INVALID_HEIGHT);
            return;
        }
        $params['height-week'] = $request->input('height-week');


        if(!$request->has('height-month') || empty($request->input('height-month')))
        {
            self::ShowError(ErrorDefine::INVALID_HEIGHT);
            return;
        }
        $params['height-month'] = $request->input('height-month');


        if(!$request->has('level') || empty($request->input('level')))
        {
            self::ShowError(ErrorDefine::INVALID_ADMIN_TYPE);
            return;
        }
        $params['level'] = $request->input('level');


        if($request->has('qq') && !empty($request->input('qq')))
        {
           $qq = $request->input('qq');

            $team = Teamqq::where("qq",$qq)->get()->toArray();

            if(!empty($team))
            {
                self::ShowError(ErrorDefine::ONLINETEAM_EXIST);
                return;
            }

        }


        if($request->has('qq_name') && !empty($request->input('qq_name')))
        {
            $qq_name = $request->input('qq_name');

            $team = Teamqq::where("username",$qq_name)->get()->toArray();

            if(!empty($team))
            {
                self::ShowError(ErrorDefine::ONLINETEAM_EXIST);
                return;
            }
        }

        $params['balance'] = 5000;

        $id = Rad_manage::insertGetId($params);


        if(!empty($qq) && !empty($qq_name))
        {

            //   初始账号  5000

            $line = [];

            $line['qq'] = $qq;
            $line['username'] = $qq_name;
            $line['manage_id'] = $id;

            $res = Teamqq::insertGetId($line);
        }


        return self::ShowMessage();

    }
    
    
    
    static function getTokenById($token)
    {
        $user = Rad_manage::where('token',$token)->get()->toArray();
        return $user;
    }





}


